<!--比价页面-->

<template>
  <div>
    <el-collapse v-model="activeName" accordion>
      <el-collapse-item title="比价分析" name="1">
        <div id="table">
          <div class="el_row_bot clearfix">
            <el-button class="fr fr_right" type="success" icon="el-icon-success" v-if='isedit' v-show="quotaIsShow ==1"  @click="quotaSetting()">配额设置</el-button>
            <el-button class="fr fr_right" type="primary" v-if='isSaveEdit' @click="submitForm">保存配额</el-button>
            <el-button class="fr fr_right" type="primary" v-if='isSaveEdit' @click="cancelQuota">取消</el-button>
            <el-button class="fr fr_right" type="success" icon="el-icon-success" @click="submitPriceCheck()" v-if="buttonIsShow ==1">提交价格审核</el-button>
          </div>
          <el-table :data="tableData" border max-height="250">
            <el-table-column prop="materialCode" fixed label="物料编码" width="150"></el-table-column>
            <el-table-column prop="materialName" fixed label="物料名称" width="200"></el-table-column>
            <el-table-column prop="purOrgName" label="采购组织名称" width="120"></el-table-column>
            <el-table-column prop="planQty" label="计划采购量" width="100"></el-table-column>
            <el-table-column prop="logistMode" label="物流方式" width="100"></el-table-column>
            <el-table-column prop="operation" label="操作" width="140">
              <template slot-scope="scope">
                <el-button @click="termComparisonClick(scope.row)" type="text" size="small">条款对比</el-button>
                <el-button @click="subentryComparisonClick(scope.row)" type="text" size="small">分项对比</el-button>
              </template>
            </el-table-column>
            <el-table-column prop="lowestPrice" label="上期最低报价" width="120"></el-table-column>
            <el-table-column v-for='item in tableConfig' :label="item.label" :prop='item.prop' :key='item.prop' :width='item.width'>
              <el-table-column v-if='(item.children||item.children.length>0)&&!item1.hidden' v-for="item1 in item.children" :label="item1.label"
                :prop='item1.prop' :width='item1.width' :key="item1.prop">
                <template slot-scope="scope">
                  <span v-if="item1.prop.indexOf('quota')<0">{{scope.row[item1.prop]}}</span>
                  <template v-else>
                    <span v-if="isedit">{{scope.row[item1.prop]}}</span>
                    <el-input type="text" v-else v-model="tableData[scope.$index][item1.prop]" />
                  </template>
                </template>
              </el-table-column>
            </el-table-column>
          </el-table>
          <template>
            <el-dialog title="分项对比" :visible.sync="dialogSubentryComparison" left width="80%">
              <Subentry-comparison :SubentryComparisonData="SubentryComparisonData" ref="SubentryComparison"></Subentry-comparison>
            </el-dialog>
          </template>
          <template>
            <el-dialog title="条款对比" :visible.sync="dialogClause" left width="80%">
              <Dialog-Clause :dialogClauseData="dialogClauseData" ref="DialogClause"></Dialog-Clause>
            </el-dialog>
          </template>
        </div>
      </el-collapse-item>
      <el-collapse-item title="已报价记录" name="2">
        <div class="quote_style">
          <quotedPrice></quotedPrice>
        </div>
      </el-collapse-item>
      <el-collapse-item title="未报价记录" name="3">
        <div class="quote_style">
          <notQuoted></notQuoted>
        </div>
      </el-collapse-item>
      <!-- <el-collapse-item title="比价对比" name="4">
        <contrast></contrast>
      </el-collapse-item> -->
    </el-collapse>
  </div>
</template>
<script>
  import QuotedPrice from './priceRatio/quotedPrice.vue';
  import NotQuoted from './priceRatio/notQuoted.vue';
  import Contrast from './priceRatio/contrast.vue';
  import DialogClause from '@/components/rfq/enquiry/enquirysteps/child/dialogClause.vue';
  import SubentryComparison from '@/components/rfq/enquiry/enquirysteps/child/subentryComparison.vue';
  import TableExt from '@/components/common/table-ext/table-ext.vue';
  import SelectTable from '@/components/common/select-ext/select-table';
  import TableIcon from '@/components/common/table-ext/table-icon';
  import qs from 'qs';

  export default {
    components: {
      QuotedPrice,
      NotQuoted,
      Contrast,
      TableIcon,
      'Subentry-comparison': SubentryComparison,
      'Dialog-Clause': DialogClause,
    },
    data() {

      let _this = this;
      return {
        quotedPrice: [],
        buttonIsShow:0,
        quotaIsShow:0,
        formInline: {},
        activeName: '1',
        dialogClause: false,
        dialogClauseData: [],
        noPublish:'',
        dialogSubentryComparison: false,
        SubentryComparisonData: [],
        isedit: true,
        isSaveEdit: false,
        activeName2: 'first',
        isShow: false,
        tableData: [],

        // tableData: [
        //     {
        //         "id": "dc9b79ec-ae45-43c0-bd78-2abe7eea2898",
        //         "closeStatus": 0,
        //         "templateType": 1,
        //         "materialCode": "132000-00031",
        //         "materialName": "双乙酰酒石酸单甘酯",

        //         "purOrgName": "集团采购",
        //         "planQty": "4.0千克",
        //         "logistMode": null,
        //         "lowestPrice": "",
        //         "venMatId_100091": "22320c26-ebdb-4554-b495-3b7065b3e2de",
        //         "firstTaxPrice_100091": 20,
        //         "lastTaxPrice_100091": 10,
        //         "quota_100091": "",
        //         "vendorCodeList": [
        //             "126133"
        //         ]
        //     }
        // ],
        tableConfig: []
      };
    },
    created() {
      var rfqId = this.$route.query.id; //询价单ID

      this.axios.post(this.lib.config.host.rfq + '/ctmgt/rfq/enquiry/rfqEnquiry/' + rfqId).then((res) => {
        var rfqMain = res.data;

         //已提交比价状态，并且提交价格审核为未提交        
        if (rfqMain.submitCompStatus == 1 && rfqMain.submitWinStatus == 0) {
          this.quotaIsShow = 1;
          this.buttonIsShow = 1;
        }
        
      }).catch(function (error) {
        console.log(error);
      });

      //比价分析-动态表头
      this.axios.post(this.lib.config.host.rfq + '/ctmgt/rfq/enquiry/compare/buildQuotaGrid/' + rfqId).then((res) => {
        if (res.data.success) {
          var dynamicColumns = res.data.result.dynamicColumns;
          this.tableConfig = dynamicColumns;
        }
      }).catch(function (error) {
        console.log(error);
      });

      //比价分析数据
      this.axios.post(this.lib.config.host.rfq + '/ctmgt/rfq/enquiry/compare/buildQuotaGridData/' + rfqId).then((res) => {
        if (res.data.success) {
          this.tableData = res.data.result;
        }
      }).catch(function (error) {
        console.log(error);
      });
      // this.init();

    },

    methods: {
      // init(){
      // //获取已报价记录
      // this.axios.post(this.lib.config.host.rfq + '/ctmgt/rfq/enquiry/compare/quoted/' + this.$route.query.id).then((res) => {
      //   if (res.data.success) {
      //     this.quotedPrice = res.data;
      //   }
      // }).catch(function (error) {
      //   console.log(error);
      // });
      // },
      //配额设置
      quotaSetting() {
        this.isedit = false; //配额设置
        this.isSaveEdit = true; //保存配额
      },
      //取消配额设置
      cancelQuota() {
        this.isedit = true; //配额设置
        this.isSaveEdit = false; //保存配额
      },
      //保存配额
      submitForm() {
        let data = [];
        let validFlag = false;
        this.tableData.forEach((item, index) => {
          
          var vendorCodeList = item.vendorCodeList; //供应商ID列表
          let suggestQuotaTotal = 0;
          validFlag = false;
          for (var i = 0; i < vendorCodeList.length; i++) {
            var venMatObj = {};
            Object.keys(item).forEach(k => {
              if (k.indexOf('venMatId_' + vendorCodeList[i]) >= 0) {
                venMatObj.id = item[k]; //询价供应商物料ID
                // console.log('venMatId_')
                // console.log(venMatObj.id)
              }
              if (k.indexOf('quota_' + vendorCodeList[i]) >= 0) {
                venMatObj.suggestQuota = item[k]; //建议配额
                suggestQuotaTotal += parseFloat(venMatObj.suggestQuota);
                // console.log('venMatId_')
                // console.log(venMatObj.id)
              }
            })
            data.push(venMatObj);
          }
          if (suggestQuotaTotal != 100) {
            validFlag = true;
            this.$message.info('物料名称：【' + item.materialName + '】的所有供应商配额合计必须为100！');
            return;
          }
        })
        if (validFlag) {
          return;
        }
        this.isedit = true; //配额设置
        this.isSaveEdit = false; //保存配额
        // console.log(data);

        //保存配额提交
        this.axios.post(this.lib.config.host.rfq + '/ctmgt/rfq/enquiry/compare/compareQuota/saveQuota', data).then((res) => {
          if (res.data.success) {
            this.$message.info('操作成功!');
          }
        }).catch(function (error) {
          console.log(error);
        });
      },
      //提交价格审核
      submitPriceCheck() {
        let data = {};
        // let priceCheckIds = [];
        // priceCheckIds.push(this.$route.query.id);
        data.priceCheckIds = this.$route.query.id;
        data.dg_checkContent = '测试审核内容';
        this.axios.post(this.lib.config.host.rfq + '/ctmgt/rfq/enquiry/compare/submitPriceCheck', qs.stringify(data)).then(
          (res) => {
            if (res.data.success) {
              this.$message.info('操作成功!');
            }
          }).catch(function (error) {
          console.log(error);
        });
      },
      //条款对比
      termComparisonClick(row) {
        this.dialogClause = true;
        row.enquiryId = this.$route.query.id;
        this.dialogClauseData = row;

      },
      //分项对比
      subentryComparisonClick(row) {
        this.dialogSubentryComparison = true;
        row.enquiryId = this.$route.query.id;
        this.SubentryComparisonData = row;


      },
      handleClick(tab, event) {
        console.log(tab, event);
      }
    }
  }

</script>

